Vert.x TCP Server
Jakob Jenkov |
Vert.x comes with a TCP server which makes it easy to create TCP servers in your application. You can create one or more TCP servers, depending on your need. In this tutorial I will look at how to create a TCP server from inside a verticle and how to handle incoming connections and data.
Creating a TCP Server
You create a Vert.x TCP server by calling the createNetServer() method on the Vertx
object. Here is how that looks:
NetServer server = vertx.createNetServer();
You will often create a TCP server from inside a verticle. Here is how you create a Vert.x TCP server from inside a verticle:
import io.vertx.core.AbstractVerticle;
import io.vertx.core.net.NetServer;
public class VertxTcpServerVerticle extends AbstractVerticle {
@Override
public void start() throws Exception {
NetServer server = vertx.createNetServer();
}
}
Starting the TCP Server
Once you have created the TCP server, you can start it using its listen() method.
Here is how starting the TCP server looks:
public class VertxTcpServerVerticle extends AbstractVerticle {
@Override
public void start() throws Exception {
NetServer server = vertx.createNetServer();
server.listen(10000);
}
}
This example starts the server and instructs it to listen on TCP port 10.000 .
The NetServer class contains more versions of the listen() method which gives you
different options for starting the TCP server.
Setting a Connect Handler on the TCP Server
In order to handle incoming TCP connections you need to set a connect handler on the TCP server. This is normally done before starting the TCP server. Here is a Vert.x TCP server connect handler example:
public class VertxTcpServerVerticle extends AbstractVerticle {
@Override
public void start() throws Exception {
NetServer server = vertx.createNetServer();
server.connectHandler(new Handler<NetSocket>() {
@Override
public void handle(NetSocket netSocket) {
System.out.println("Incoming connection!");
}
});
server.listen(10000);
}
}
The handle() method of the connect handler is called whenever a new TCP connection is created by
a client of the TCP server. The NetSocket object passed as parameter to the handle()
method gives access to the incoming connection (socket etc.).
Reading Data From The Socket
In order to read data from the incoming connections you need to set a handler on the NetSocket object
for the connection. Here is how that is done:
public class VertxTcpServerVerticle extends AbstractVerticle {
@Override
public void start() throws Exception {
NetServer server = vertx.createNetServer();
server.connectHandler(new Handler<NetSocket>() {
@Override
public void handle(NetSocket netSocket) {
System.out.println("Incoming connection!");
netSocket.handler(new Handler<Buffer>() {
@Override
public void handle(Buffer buffer) {
System.out.println("incoming data: "+buffer.length());
buffer.getString(0,buffer.length());
}
});
}
});
server.listen(10000);
}
}
Writing Data to The Socket
Once you have processed the incoming data you may want to write a response back to the client. You can do so
via the NetSocket instance passed to the connect handler's handle() method.
Here is an example of writing data back to the client via the NetSocket:
public class VertxTcpServerVerticle extends AbstractVerticle {
@Override
public void start() throws Exception {
NetServer server = vertx.createNetServer();
server.connectHandler(new Handler<NetSocket>() {
@Override
public void handle(NetSocket netSocket) {
System.out.println("Incoming connection!");
netSocket.handler(new Handler<Buffer>() {
@Override
public void handle(Buffer inBuffer) {
System.out.println("incoming data: " + inBuffer.length());
String data =
inBuffer.getString(0, inBuffer.length());
Buffer outBuffer = Buffer.buffer();
outBuffer.appendString("response...");
netSocket.write(outBuffer);
}
});
}
});
server.listen(10000);
}
}
The write() method of the NetSocket method is asynchronous. Thus, it returns immediately.
The data will be written to the underlying socket at a later time.
The NetSocket class contains more versions of the write() method which enables you to
write e.g. a String directly to the socket without using a Buffer. See the Vert.x JavaDoc for more
details.
Closing The TCP Server
When you are finished with the TCP server you can close it using its close() method. Here is
how closing a Vert.x TCP server looks:
server.close();
Closing the TCP server is an asynchronous actions, so the close() method may exit before the TCP
server is actually closed. If you need to be notified of when the TCP server has shut down, you can pass a
Handler to the close() method. This handler will then be called when the TCP server
is fully closed. Here is how that looks:
server.close(new Handler<AsyncResult<Void>>() {
@Override
public void handle(AsyncResult result) {
if(result.succeeded()){
//TCP server fully closed
}
}
});
| Tweet | |
Jakob Jenkov | |









